package edu.jd.login;

import edu.jd.common.Utils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class LoginService {

    public boolean isLogin(){
        return CurrentUser.getCurrentUser() != null;
    }

    public boolean login(String user_id, String password){
        SqlSession sess = Utils.openSession();
        try{
            LoginDao dao = sess.getMapper(LoginDao.class);
            List<User> userList = dao.getUserByIdAndPassword(user_id, password);
            sess.commit();
            if(userList.size()!=0){
                CurrentUser.setCurrentUser(userList.get(0));
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
            sess.rollback();
            throw new RuntimeException("获取用户信息失败");
        }finally {
            sess.close();
        }
    }
}
